package io.shqyang.myapplication.slice;

import io.shqyang.myapplication.ResourceTable;
import io.shqyang.myapplication.time.TimeManager;
import io.shqyang.myapplication.time.TimeObserver;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Image;
import ohos.agp.components.Text;

import java.util.Arrays;


public class MainAbilitySlice extends AbilitySlice implements TimeObserver {

    private Text hourTt;
    private Text minuteTt;
    private Text secTt;
    private Image bgImg;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        hourTt = (Text) findComponentById(ResourceTable.Id_hou_tt);
        minuteTt = (Text) findComponentById(ResourceTable.Id_min_tt);
        secTt = (Text) findComponentById(ResourceTable.Id_sec_tt);
        bgImg = (Image) findComponentById(ResourceTable.Id_img_bg);
        TimeManager.getInstance().addObserver(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        TimeManager.getInstance().removeObserver(this);
        super.onStop();
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void updateSecond(String time) {

    }

    @Override
    public void updateSecond(String[] timeArr) {
        getUITaskDispatcher()
                .asyncDispatch(()->{
                    if (timeArr[2].equals("00")) {
                        watchOut();
                    }
                    hourTt.setText(timeArr[0]);
                    minuteTt.setText(timeArr[1]);
                    secTt.setText(timeArr[2]);
                });
    }

    private void watchOut() {
        AnimatorProperty propertyImg = bgImg.createAnimatorProperty();
        propertyImg.rotate(360);
        propertyImg.setDuration(1000);
        propertyImg.setLoopedCount(0);
        propertyImg.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                propertyImg.reset();
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
        propertyImg.start();
    }
}
